package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONArray;
import com.juyu.miliao.api.dao.mysql.OfficialQuestionInfoDao;
import com.juyu.miliao.api.dao.redis.OfficialQuestionInfoRedisDao;
import com.juyu.miliao.api.dao.redis.OfficialQuestionToAnsweRedis;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.OfficialQuestionInfo;
import com.juyu.miliao.api.domain.OfficialQuestionToAnswerInfo;
import com.juyu.miliao.api.domain.vo.OfficialQuestionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 官方问题信息service
 *
 * @author xuanqicheng
 * @create 2018-18-10:26 AM
 */
@Service
public class OfficialQuestionInfoService {


    @Autowired
    private OfficialQuestionToAnsweRedis officialQuestionToAnsweRedis;

    @Autowired
    private OfficialQuestionInfoRedisDao officialQuestionInfoRedisDao;

    @Autowired
    private UserInfoCache userInfoCache;

    public OfficialQuestionVo getOneOfficalQuestion() {

        OfficialQuestionVo officialQuestionVo = new OfficialQuestionVo();

        OfficialQuestionInfo officialQuestionInfo = officialQuestionInfoRedisDao.getNewOfficialQuestionInfo();

        if(officialQuestionInfo !=null){

            setOfficialQuestionVo(officialQuestionVo, officialQuestionInfo);

        }

        return officialQuestionVo;
    }

    public List<OfficialQuestionVo> getOfficialQuestionInfoList(Integer page, Integer pageSize) {

        List<OfficialQuestionInfo> list = officialQuestionInfoRedisDao.getOfficialQuestionInfoList(page,pageSize);

        List<OfficialQuestionVo> officialQuestionVoList = new ArrayList<>();

        for(OfficialQuestionInfo officialQuestionInfo : list){

            OfficialQuestionVo officialQuestionVo = new OfficialQuestionVo();

            setOfficialQuestionVo(officialQuestionVo,officialQuestionInfo);

            officialQuestionVoList.add(officialQuestionVo);
        }
        return officialQuestionVoList;
    }

    /**
     * 设置OfficialQuestionVo
     * @param officialQuestionVo
     * @param officialQuestionInfo
     */
    private void setOfficialQuestionVo(OfficialQuestionVo officialQuestionVo, OfficialQuestionInfo officialQuestionInfo) {
        officialQuestionVo.setId(officialQuestionInfo.getId());

        officialQuestionVo.setOfficialQuestionUserId(officialQuestionInfo.getQuestionUserId());

        UserCacheVo userCacheVo = userInfoCache.getUserCacheInfo(officialQuestionInfo.getQuestionUserId());

        if(userCacheVo !=null){
            officialQuestionVo.setIcon(userCacheVo.getIcon());

            officialQuestionVo.setQuestionIsVip(userCacheVo.getIsVip());
        }else{
            officialQuestionVo.setIcon("");
            officialQuestionVo.setQuestionIsVip(1);
        }

        officialQuestionVo.setOfficalTitle(officialQuestionInfo.getTitle());

        officialQuestionVo.setOfficalQuestionContent(officialQuestionInfo.getQuestionContent());



        OfficialQuestionToAnswerInfo officialQuestionToAnswerInfo = officialQuestionToAnsweRedis
                .getNewOfficialQuestionToAnswerInfo(officialQuestionInfo.getId());

        if(officialQuestionToAnswerInfo!=null){

            Map<String,Object> detailMap = new HashMap<>(0);

            UserCacheVo answerUserInfo = userInfoCache.getUserCacheInfo(officialQuestionToAnswerInfo.getAnswerUserId());

            detailMap.put("userId",answerUserInfo.getUserId());

            detailMap.put("nickName",answerUserInfo.getNickName());

            detailMap.put("icon",answerUserInfo.getIcon());

            detailMap.put("sex",answerUserInfo.getSex());

            detailMap.put("age",answerUserInfo.getAge());

            detailMap.put("mGrade",answerUserInfo.getMGrade());

            detailMap.put("vGrade",answerUserInfo.getVGrade());
            detailMap.put("answerIsVip",answerUserInfo.getIsVip());
            detailMap.put("answerIsHost",answerUserInfo.getIsHost());
            detailMap.put("answerVipLevel",answerUserInfo.getVipLevel());
            detailMap.put("answerStar",answerUserInfo.getStar());
            detailMap.put("answerTime",officialQuestionToAnswerInfo.getAnswerTime());
            detailMap.put("answerUrl",officialQuestionToAnswerInfo.getAnswerUrl());
            detailMap.put("id",officialQuestionToAnswerInfo.getId());

            long number = officialQuestionToAnsweRedis.getEavesdroppingNumber(officialQuestionInfo.getId(),
                    officialQuestionToAnswerInfo.getId());
            //偷听测试
            detailMap.put("eavesdroppingNumber",number);

            JSONArray jsonArray =officialQuestionToAnsweRedis
                    .getOfficialQuestionToAnswerByIconCache(officialQuestionInfo.getId());

            if(jsonArray!=null&& jsonArray.size()>0){
                if(jsonArray.size()<=3){
                    detailMap.put("answerIcons",jsonArray);
                }else{
                    detailMap.put("answerIcons",jsonArray.subList(0,3));
                }
            }else{
                detailMap.put("answerIcons",new JSONArray());
            }

            officialQuestionVo.setDetailAnswers(detailMap);

        }
    }


    public OfficialQuestionVo getOfficialQuestionByQuestionId(Integer questionId) {

        OfficialQuestionInfo officialQuestionInfo = officialQuestionInfoRedisDao
                .getOfficialQuestionByQuestionId(questionId);

        OfficialQuestionVo officialQuestionVo = new OfficialQuestionVo();

        if(officialQuestionInfo !=null){

            setOfficialQuestionVo(officialQuestionVo, officialQuestionInfo);


        }
        officialQuestionVo.setDetailAnswers(new HashMap<>(0));
        return officialQuestionVo;
    }
}
